<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="com.jframe.sys.utils.StringUtil" %>
<%
int id = StringUtil.parseInt(request.getParameter("id"));
String contextPath = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<jsp:include page="/inc/editInc.jsp" />
<script type="text/javascript" charset="utf-8" src="<%=contextPath %>/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="<%=contextPath %>/js/ueditor/ueditor.all.min.js"> </script>
<script type="text/javascript" charset="utf-8" src="<%=contextPath %>/js/ueditor/lang/zh-cn/zh-cn.js"></script>
<jsp:include page="/inc/basedwr.jsp" />
<script type="text/javascript">
var action = 'docTextAction';

$(document).ready(function(){
	//实例化编辑器
	 var contentEditor = new UE.ui.Editor({
			UEDITOR_HOME_URL:contextPath + '/js/ueditor/',
			autoHeightEnabled:true
		   }); 
	 contentEditor.render("content");
	
	$.ajax({
		url: action+'.do?method=getObjById',
		type: 'post',
		data:{id:$('#id').val()},
		dataType:'json',
	    success: function(data){	
	    	var obj = data.obj;
	    	var leadIng = obj.leadImg;
	    	$('#data').form('load',obj);
	    	UE.getEditor('content').ready(function(){ 
	    		contentEditor.setContent(data.content);//这里给编辑器添加内容 
            });
	    	$('#uploadImg').attr('src','<%=contextPath%>'+leadIng);
	    }
	});
	
	var saveForm = function(){
		var validate = $('#data').form('validate');
		var content = contentEditor.getContent();
		if(content==null||content==''){
			alertInfo("请输入内容");
			return;
		}
		var title = $('#title').val();
		if(title==''||title==null){
			alertInfo("请输入标题");
			return;
		}		
		var typeId = $('#typeId').combotree('getValue');
		if(title==''||title==null||typeId==0||typeId=='0'){
			alertInfo("请选择文章类别");
			return;
		}		
		if(validate){
			$.ajax({
				url:action+".do?method=save",
				data:{obj:$("#data").form2json(),content:content},
				dataType:'json',
				type:'post',
				success:function(data){
					if(data.result==true){
						$('#data').form('load',data.obj);
						alertInfo('保存成功！');
						parent.$('#grid').datagrid('reload');
					}
					else{
						alertError('保存失败！');
					}
				}
			});
		}else{
			return validate;
		}
	};
	$('#saveBtn').click(saveForm);
	
	$("#typeId").combotree({
		 onSelect : function(node) {  
	        //返回树对象  
	         var tree = $(this).tree;  
	        //选中的节点是否为叶子节点,如果不是叶子节点,清除选中  
	        var isLeaf = tree('isLeaf', node.target);  
	         if (!isLeaf) {  
	             //清除选中  
	             $('#typeId').combotree('clear');  
	             alertInfo("请选择子类型");
	         }  	        
		 }  
	});
	
	 var leadImgUploadBtn = function(){ 
		 var fileDir ='cms/doc/pic';
		 uploadLeadPic(fileDir,'leadImg','uploadFile','uploadImg');
		 };
	 $('#uploadBtn').click(leadImgUploadBtn);
	 
	 var setStatus =function (){
			var ids = $('#id').val();
			var status=$('#status').val();	
			if(ids=='0'||ids==0){
				alertInfo("请保存后再进行操作。");
				return;
			}
			if(status=='2'||status==2){
				alertInfo("该文章已经发布");
				return;
			}else{
				status='2';
			}
			var oprate="发布";
			if(ids==""){
				alertError("请您选择要操作的记录！");
				return;
			}else{
				econfirm('警告','确定要'+oprate+'该文章？',function(r){
		    		if(r){
		    			var url = action+".do?method=setStatus";
		    			$.ajax({
		    				url:url,
		    		 		type: 'post',
		    			    data:{
		    			    	ids:ids,
		    			    	status:status
		    			    },
		    			    dataType: 'json',
		    			    error: function(){
		    			        alertError(oprate+'失败!');
		    			    },
		    			    success: function(data){
		    			    	alertInfo('成功'+oprate+'所选文章！');
		    			    	$('#status').val(status);
		    			    	parent.$('#grid').datagrid('reload');	    			    	
		    			    }
		    			});
		    		}
		    	});
				
			}
		}
	 
	 $('#publishBtn').click(setStatus);
	 
	 function showDoc(){
		 var id = $('#id').val();
		 if(id=='0'||id==0){
				alertInfo("请保存后再进行操作。");
				return;
		 }
		 var url = contextPath+"/web/docInfo.jsp?id="+id;
	 }
	 $('#publishBtn').click(showDoc);
});
</script>
</head>
<body>
    <form id="data" method="post">
    	<input type="hidden" id="id" name="id" value="<%=id%>"/>
    	<input type="hidden" id="status" name="status" />
    	<input type="hidden" id="isTop" name="isTop" />
    	<input type="hidden" id="docUrl" name="docUrl" />
    	<input type="hidden" id="visitNum" name="visitNum" />
	    <table width="100%" class="tableForm">
	    	<tr>
	    		<td align="right" width="30%">标<span style="padding-left: 14px;">题：</span><input type="text" id="title" name="title"  style="width:677px;" class="easyui-validatebox"/></td>	 
	    		<td align=left width="30%">
	    			<span style="padding-left: 40px;"><a id='saveBtn' class="easyui-linkbutton" plain="true" iconCls="icon-save">保存</a>&nbsp;
	    			<a id='publishBtn' class="easyui-linkbutton" plain="true" iconCls="icon-redo">发布</a>&nbsp;<a id='showBtn' class="easyui-linkbutton" plain="true" iconCls="icon-redo">预览</a>&nbsp;</span>
	    		</td>
	    	</tr>
	    	<tr>
	    		<td align="right" width="70%">
	    			副标题：<input type="text" id="shortTitle" name="shortTitle"  style="width:490px;" class="easyui-validatebox"/>
	    			文章类别：<select id="typeId" name="typeId"  class="easyui-combotree" style="width:125px;" url="<%=request.getContextPath()%>/docTextAction.do?method=getCombotree&key=sm_cms_docType"></select>
	    		</td>
	    		<td align="left"  width="30%">
	    			作<span style="padding-left: 25px;">者：</span><input type="text" id="author" name="author" class="easyui-validatebox"/>
	    		</td>   	
	    	</tr>
		    <tr>
		    	<td align="right">
		    		<script id="content" type="text/plain" style="width:750px;height:750px;"></script>	
		    	</td>	
		    	<td width="30%" align="left" style="vertical-align:top">
		    		<div>
				    	<table>
				    		<tr>
				    			<td>发布日期：<input id="pubTime" name="pubTime" type="text" class="easyui-datebox" style="width:153px;"></input></td>
				    		</tr>
				    		<tr>
				    			<td>归档日期：<input id="pubEndTime" name="pubEndTime" type="text" class="easyui-datebox" style="width:153px;"></input></td>
				    		</tr>
				    		<tr>
					    		<td>文章简介：<br/>
					    			<textarea id="summary" name="summary" style="width: 207px;height:300px"></textarea>
					    		</td>
				    		</tr>
				    		<tr>
				    			<td align="left">
				    				引导图：<span style="padding-left: 85px;"><a id='uploadBtn' class="easyui-linkbutton" plain="true" iconCls="icon-upload">上传图片</a></span>
				    			</td>
				    		</tr>
				    		<tr>
				    			<td>
				    				<img alt="引导图" src="#" style="width:210px;height:180px;" id="uploadImg"/>
				    				<input type="hidden" id="leadImg" name="leadImg"/>
				    			</td>
				    		</tr>				    		
				    		<tr>
				    			<td>
				    				<input type="file" id="uploadFile" name="uploadFile"/>
				    			</td>
				    		</tr>
				    		
				    	</table>
			    	</div>   
		    	</td>		    	 
		    </tr>
			<tr align="center">
				<td colspan="2">
					<a id='saveBtn' class="easyui-linkbutton" plain="true" iconCls="icon-save">保存</a>
				</td>
			</tr>
		</table>
    </form>
</body>
</html>